package com.cy.controller;


import com.cy.bfzcommons.pojo.Orders;
import com.cy.bfzcommons.service.OrdersService;
import com.cy.bfzcommons.web.util.JsonResult;
import com.cy.bfzcommons.web.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@CrossOrigin

public class OrdersController {


    @Autowired
    private OrdersService ordersService;

    // 查询所有订单
    @GetMapping("/ordersId")
    public JsonResult getOrders(){
        List<Orders> list = ordersService.getOrders();
        return JsonResult.ok(list);
    }


    // 订单修改
    @PutMapping("/updateOrders")
    public JsonResult upOrders(@ApiParam(name="orders",value="订单信息",required=true)@RequestBody Orders orders){
        ordersService.upOrders(orders);
        return JsonResult.ok();
    }

    // 订单新增
    @PostMapping("/addOrders")
    public JsonResult addOrders(@ApiParam(name="orders",value="订单信息",required=true)@RequestBody Orders orders){
        Integer ordersId = ordersService.addOrders(orders);
        return JsonResult.ok(ordersId);
    }

    // 订单删除
    @DeleteMapping()
    public JsonResult deleteOrders(@ApiParam(name="ordersId",value="订单id",required=true)Integer ordersId){
        ordersService.deleteOrders(ordersId);
        return JsonResult.ok();
    }


    @GetMapping("/ordersId2")
    public JsonResult getOrders2(Integer orderNumber){
        return JsonResult.ok( PageUtil.startpage().doSelectPageInfo(()->
                ordersService.getOrders2(orderNumber)));
    }


    @PutMapping("/status/{ordersId}/{status}")
    public JsonResult updateStatus(@PathVariable Integer ordersId,@PathVariable Integer status){
        Integer integer = ordersService.updateStatus(ordersId, status);
        if(integer==1){
            return JsonResult.ok();
        }
        return JsonResult.fail();

    }


    @GetMapping("vue/{ordersId}")
    public JsonResult selectOrderById(@ApiParam(name="ordersId",value="订单id",required=true)@PathVariable Integer ordersId){
        Orders orders =  ordersService.selectOrderById(ordersId);
        return JsonResult.ok(orders);
    }

}
